package cn.leaf.calculator.resolver;

import cn.leaf.calculator.bean.ParameterBeanDefinition;

import lombok.NonNull;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * JS 公式解析器
 * <p变量必须符合js变量命名规范，在本解析器中只识别_param_形式的变量，即前后需加入下划线，并且变量中无空格，-等特殊字符></p>
 *
 * @author 李克国
 * @version 1.0.0

 */
public class JSFormulaBeanResolver<T> implements FormulaBeanDefinitionResolver<T> {

    public static final String PARAM = "(?<=_)(.+?)(?=_)";

    @Override
    public Map<Serializable, ParameterBeanDefinition<T>> resolve(@NonNull String formulaString) {
        Assert.notNull(formulaString, "需要解析的公式不能为空");

        Matcher matcher = Pattern.compile(PARAM).matcher(formulaString);
        Map<Serializable, ParameterBeanDefinition<T>> result = new HashMap<>(16);
        while (matcher.find()) {
            String group = matcher.group();
            if ( group.trim().matches("[-\\+/\\*%:\\?\\(\\)]")) {
                System.out.println(group);
                continue;
            }
            if ("==".equals(group)) {
                continue;
            }
            result.put(group, null);
        }
        return result;
    }

    @Override
    public void refresh() {

    }

    @Override
    public boolean validate() {
        return false;
    }
}
